package manager.customer.controller;
import manager.customer.service.CustomerService;
import manager.customer.vo.PositionVO;
import manager.customer.vo.ReceiverVO;
import manager.customer.vo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(value = {"/customer"})
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 校验的目的仅仅在于看此用户名有没有注册过
     * @param data
     * @param type
     * @return
     */
    @GetMapping("user/check/{data}/{type}")
    public ResponseEntity<Boolean> check(@PathVariable("data") String data, @PathVariable("type") Integer type){

        Boolean bool = customerService.checkData(data,type);
        if (null==bool){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        return ResponseEntity.ok(bool);
    }

    @PostMapping("user/code")
    public ResponseEntity<Void> sendSms(@RequestParam("phone") String phone){
        Boolean bool = customerService.sendSms(phone);
        if (null==bool || !bool){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        return ResponseEntity.ok().build();
    }

    @PostMapping("user/register")
    public ResponseEntity<Void> register(@RequestBody Customer customer){
        Boolean bool = customerService.register(customer);
        if (null==bool||!bool){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        return ResponseEntity.ok().build();
    }
    @PostMapping("user/position/list")
    public ResponseEntity<List<PositionVO>> positionList(@RequestBody PositionVO positionVO){
        List<PositionVO> positionVOList = this.customerService.getPositionByCondition(positionVO);
        if (null== positionVOList){
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(positionVOList);
    }
    //根据条件查询用户信息
    @PostMapping("user/list")
    public ResponseEntity< List<Customer>> listUser(@RequestBody Customer customer){
        List<Customer> storeCustomer = this.customerService.listUser(customer);
        if (null== storeCustomer){
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(storeCustomer);
    }
    //添加收货人信息
    @PostMapping("user/receiver/add")
    public ResponseEntity<Void> addReceiver(@RequestBody ReceiverVO receiverVO){
        Boolean bool = customerService.addReceiver(receiverVO);
        if (null==bool||!bool){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        return ResponseEntity.ok().build();
    }
    //根据用户查询收货人信息
    @PostMapping("user/receiver/list")
    public ResponseEntity<List<ReceiverVO>> getReceiverByCondition(@RequestBody ReceiverVO receiverVO){
        List<ReceiverVO> listReceiver = customerService.getReceiverByCondition(receiverVO);
        return ResponseEntity.ok(listReceiver);
    }
    //更新收货地址
    @PostMapping("user/receiver/update")
    public ResponseEntity<List<ReceiverVO>> updateReceiver(@RequestBody ReceiverVO receiverVO){
        customerService.updateReceiver(receiverVO);
        return ResponseEntity.ok().build();
    }
    //删除收货地址
    @PostMapping("user/receiver/delete")
    public ResponseEntity<Void> deleteReceiver(@RequestBody ReceiverVO receiverVO){
        customerService.deleteReceiver(receiverVO);
        return ResponseEntity.ok().build();
    }
}
